package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.juzi.wopu.domain.Menu;
import com.juzi.wopu.domain.Role;
import com.juzi.wopu.domain.RoleMenu;
import com.juzi.wopu.domain.RoleMenuResponse;
import com.juzi.wopu.query.RoleMenuQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.service.IMenuService;
import com.juzi.wopu.service.IRoleMenuService;
import com.juzi.wopu.service.IRoleService;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class RoleMenuAction extends CRUDAaction<RoleMenu> {
	
	private static final long serialVersionUID = 1L;

	private IRoleMenuService roleMenuService;
	private IRoleService roleService;
	private IMenuService menuService;

	private RoleMenu roleMenu = new RoleMenu();
	
	private RoleMenuQuery baseQuery = new RoleMenuQuery();
	
	private PageList pageList;
	
	private List<RoleMenuResponse> data=new ArrayList<>();
	
	public void setRoleMenuService(IRoleMenuService roleMenuService) {
		this.roleMenuService = roleMenuService;
	}
	
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	
	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}
	
	@Override
	protected void list() {
		Map<Long, Role> roleMap=new HashMap<>();
		roleService.getRolerMap(roleMap, null);
		
		Map<Long, Menu> menuMap=new HashMap<>();
		menuService.getMenuMap(menuMap, null);
		this.pageList = roleMenuService.findByQuery(baseQuery);
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			for(int i=0;i<pageList.getRows().size();i++){
				RoleMenu roleMenu=(RoleMenu) pageList.getRows().get(i);
				RoleMenuResponse response=new RoleMenuResponse(roleMenu);
//				response.setRoleName(roleMap.get(roleMenu.getId()).getsName());
//				response.setMenuName(menuMap.get(roleMenu.getId()).getName());
				
				Long roleId = roleMenu.getRole_id();
				Role role = roleMap.get(roleId);
				
				Long menuId = roleMenu.getMenu_id();
				Menu menu = menuMap.get(menuId);
				if(menu == null){
					response.setRoleName("");
				}else{
					
					String getsName = role.getsName();
					response.setRoleName(getsName);
				}
				
				if(menu== null){
					response.setMenuName("");
				}else{
					String name = menu.getName();
					response.setMenuName(name);
				}
				data.add(response);
			}
		}
	}

	@Override
	public String input() throws Exception {
		List<Role> roles=roleService.getAll();
		List< Menu> menus=menuService.getAll();
		putContext("roles", roles);
		putContext("menus", menus);
		return INPUT;
	}

	public void validateSave() {
//		if (StringUtils.isBlank(roleMenu.getName())) {
//			addFieldError("name", "名称不能为空");
//		}
	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		roleMenuService.save(roleMenu);
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				roleMenuService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {
			roleMenu = roleMenuService.get(id);
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			roleMenu = new RoleMenu();
		} else {
			roleMenu = roleMenuService.get(id);
		}
	}

	@Override
	public RoleMenu getModel() {
		return roleMenu;
	}

	public PageList getPageList() {
		return pageList;
	}

	public RoleMenuQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(RoleMenuQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public List<RoleMenuResponse> getData() {
		return data;
	}

	public void setData(List<RoleMenuResponse> data) {
		this.data = data;
	}	
}
